package cn.xaut.二叉树;

import cn.xaut.剑指offer.m20.TreeNode;

/**
 * 110. 平衡二叉树
 */
public class demo110 {

    public boolean isBalanced(TreeNode root) {

        if (root == null)
            return true;

        return Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }
    
    private int getHeight(TreeNode node) {
        
        // 递归终止条件
        if (node == null)
            return 0;

        return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
    }
}
